import fs from 'fs'
import * as cheerio from 'cheerio'
import { minify } from 'html-minifier'

const htmlPath = './dist/index.html' // 打包后的html文件路径
const htmlText = fs.readFileSync(htmlPath)
const $ = cheerio.load(htmlText)

$('script[type="module"]').removeAttr('type')
$('[crossorigin]').removeAttr('crossorigin')
$('script[nomodule]').removeAttr('nomodule')

fs.writeFileSync(
  htmlPath,
  minify($.html(), {
    removeComments: true,
    collapseWhitespace: true,
    minifyJS: true,
    minifyCSS: true
  }),
  {
    encoding: 'utf-8'
  }
)
